package com.gtxd.controller;

import java.util.Date;
import java.util.List;
import java.util.Map;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.gtxd.entity.Patientinfo;
import com.gtxd.utils.CardUtil;
import com.gtxd.utils.DateUtils;
import com.gtxd.utils.PageParams;
import com.gtxd.utils.PageUtils;
import com.gtxd.utils.R;
import com.gtxd.utils.gson.PerfectGson;

/**
 * InnoDB free: 233472 kB
 * 
 * @author yangcheng
 * @email 717449318@qq.com
 * @date 2018-03-18 08:57:56
 */
@RestController
@RequestMapping("patientinfo")
public class PatientinfoController extends BaseControl {

	/**
	 * 列表
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/list")
	public R list(@RequestParam Map<String, Object> params) throws Exception {
		// 查询列表数据
		PageParams query = new PageParams(params);
		// 获取完美gson
		Gson gson = PerfectGson.getGson();

		List<Object> patientinfoList = commonService.query(new Patientinfo(), query);
		// json转为带泛型的list
		List<Patientinfo> reList = gson.fromJson(gson.toJson(patientinfoList), new TypeToken<List<Patientinfo>>() {
		}.getType());
		int total = (int) commonService.queryRecordNum(new Patientinfo(), "", "");

		PageUtils pageUtil = new PageUtils(reList, total, query.getPageSize(), query.getCurrentPage());

		return R.ok().put("page", pageUtil);
	}

	/**
	 * 列表
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/screening")
	public R screening(@RequestParam Map<String, Object> params) throws Exception {

		return patientinfoService.screening(params);

	}

	/**
	 * 信息
	 * 
	 * @throws Exception
	 */
	@RequestMapping("/info/{pno}")
	public R info(@PathVariable("pno") String pno) throws Exception {
		// 获取完美gson
		Gson gson = PerfectGson.getGson();
		// json转为带泛型的list
		Patientinfo patientinfo = gson.fromJson(gson.toJson(commonService.query(pno, new Patientinfo())),
				new TypeToken<Patientinfo>() {
				}.getType());
		return R.ok().put("patientinfo", patientinfo);
	}

	/**
	 * 保存
	 */
	@RequestMapping("/save")
	public R save(@RequestBody Patientinfo patientinfo) throws Exception {
		// 同一天，同一医师最多接诊2名病人
		int num = commonService.queryByHql("select pno from Patientinfo where dno='" + patientinfo.getDno()
				+ "' and treatdate='" + DateUtils.dateToStr(new Date()) + "'").size();
		if (num < 2) {
			patientinfo.setTreatdate(new Date());
			patientinfo.setAge((int) CardUtil.getCarInfo(patientinfo.getIdentity()).get("age"));
			patientinfo.setPsex((int) CardUtil.getCarInfo(patientinfo.getIdentity()).get("sex"));
			commonService.save(patientinfo);
			return R.ok();
		} else {
			return R.error("今天，该主治医生超过接诊人数");
		}

	}

	/**
	 * 修改
	 */
	@RequestMapping("/update")
	public R update(@RequestBody Patientinfo patientinfo) throws Exception {
		commonService.update(patientinfo);

		return R.ok();
	}

	/**
	 * 删除
	 */
	@RequestMapping("/delete")
	public R delete(@RequestBody String[] pnos) throws Exception {
		commonService.deletebatch(new Patientinfo(), pnos);

		return R.ok();
	}

}
